<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    on-ok="handleOk"
    switch-fullscreen
    :footer="null"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading" style="margin-bottom: 20px">
      <ants-form-container :disabled="formDisabled">
        <a-form slot="detail" :form="form">
          <a-row>
            <a-col :span="24">
              <a-form-item label="名称" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"/>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="描述" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-textarea v-decorator="['describe']" :rows="4" placeholder="请输入描述"/>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </ants-form-container>
    </a-spin>
    <div
      :style="{
          position: 'absolute',
          right: 0,
          bottom: 0,
          width: '100%',
          borderTop: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
          textAlign: 'right',
          zIndex: 1,
        }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="handleCancel">
        关闭
      </a-button>
      <a-button type="primary" @click="handleOk">
        提交
      </a-button>
    </div>
  </a-modal>
</template>

<script>
import AntsFormContainer from '@/components/ants/AntsFormContainer'
import { getAction, postAction } from '@/api/manage'
import pick from 'lodash.pick'

export default {
  name: 'MyFavoritesForm',
  components: {
    AntsFormContainer
  },
  data() {
    return {
      title: '登录',
      width: 600,
      visible: false,

      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入名称!' }
          ]
        }
      },
      url: {
        add: '/articleFavorites/add'
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    }
  },
  methods: {
    add() {
      this.form.resetFields()
      this.model = {}
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name', 'describe'))
      })
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          const formData = Object.assign(this.model, values)

          postAction(this.url.add, formData).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.handleCancel()
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
